package com.netease.nie.base.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;

import com.netease.nie.base.exceptions.UnexpectedException;

/**
 * 
 * @author KingsHunter
 * @createDate May 31st,2016
 *
 */
public final class CopyUtils {

	public static void copy(Object dest, Object orig) {
		try {
			BeanUtils.copyProperties(dest, orig);
		} catch (IllegalAccessException e) {
			//e.printStackTrace();
		} catch (InvocationTargetException e) {
			//e.printStackTrace();
		}
	}
	
	/**
	 * 将origin类的List映射为dest类的List
	 * @param destList 目标类列表
	 * @param originList 原始类列表
	 * @param destClass 目标类class
	 */
	public static <K, T> List<K> copyOriginListToDestList(List<T> originList, Class<K> destClass) {
		if (CollectionUtils.isNotEmpty(originList)) {
			int size = originList.size();
			List<K> destList = new ArrayList<K>(size);
			for (int i = 0; i < size; i++) {
				T t = originList.get(i);
				if (t == null)
					throw new UnexpectedException("entity is a null object");
				try {
					K k = destClass.newInstance();
					copy(k, t);
					destList.add(k);
				} catch (InstantiationException | IllegalAccessException e) {
					//
				}
			}
			return destList;
		}
		return Collections.emptyList();
	}
	
}
